package cn.bytepath.org.controller;

//import cn.bytepath.base.controller.BaseController;
import cn.bytepath.base.controller.BaseController;
import cn.bytepath.org.domain.Shop;
import cn.bytepath.org.domain.Shop;
import cn.bytepath.org.query.ShopQuery;
import cn.bytepath.org.service.IShopService;
import cn.bytepath.util.PageInfo;
import cn.bytepath.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商户的控制层
 * @author Jack.C
 * @createDate 2022-02-10
 * @version 1.0
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "商户接口",description = "商户接口详细描述")
public class ShopController extends BaseController {

    /**
     * 注入商户业务层对象
     */
    @Autowired
    private IShopService shopService;

    @ApiOperation("查询所有Shop对象")
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    @ApiOperation("将商户状态改为禁用")
    @GetMapping("/forbid/{id}")
    public PageResult forbid(@PathVariable("id") Long id) {
        try {
            shopService.forbid(id);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("将商户状态从审核改为启用")
    @PostMapping("/permit")
    public PageResult permit(@RequestBody Shop shop) {
        try {
            shopService.permit(shop);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("驳回商户的入驻申请")
    @PostMapping("/auditback")
    public PageResult auditBack(@RequestBody Shop shop) {

        try {
            shopService.auditBack(shop);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("高级查询并分页")
    @PostMapping
    public PageInfo<Shop> pageQuery(@RequestBody ShopQuery query) {
        return shopService.findPageInfoByQuery(query);
    }

    @ApiOperation("根据id删除商户对象")
    @DeleteMapping("/{id}")
    public PageResult delete(@PathVariable("id") Long id) {

        try {
            shopService.delete(id);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("根据id批量删除商户对象")
    @PatchMapping
    public PageResult deleteBatch(@RequestBody Long[] ids) {

        try {
            shopService.deleteBatch(ids);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "商户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public PageResult saveOrUpdate(@RequestBody Shop Shop) {
        try {
            shopService.saveOrUpdate(Shop);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "商户入驻",notes = "商户入驻时执行,将信息保存至shop表和employee表")
    @PostMapping("/settlement")
    public PageResult setlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            System.out.println(shop);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @Override
    @PostMapping("/upload")
    @ApiOperation(value = "商户logo图片文件上传",notes = "商户入驻时执行,将logo保存至fastdfs")
    public PageResult upload(@RequestBody MultipartFile file) {
        return super.upload(file);
    }

    @Override
    @DeleteMapping("/delfile/{fileName}")
    @ApiOperation(value = "商户logo图片文件删除",notes = "将保存到fastdfs的logo图片删除")
    public PageResult deleteFile(@PathVariable("fileName") String fileName) {
        return super.deleteFile(fileName);
    }
}
